package api

import (
	"gateway/basic/config"
	user "gateway/basic/proto/userproto"
	"gateway/handler/request"
	"gateway/handler/response"
	"github.com/gin-gonic/gin"
)

// GetRiskRules 获取风控规则列表
// @Summary 获取风控规则列表
// @Description 根据条件获取风控规则列表
// @Tags 风控规则引擎
// @Accept json
// @Produce json
// @Param request query request.GetRiskRulesRequest true "请求参数"
// @Success 200 {object} response.SuccessResponse{data=[]request.RiskControlRule}
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/risk_control/rules [get]
func GetRiskRules(c *gin.Context) {
	var req request.GetRiskRulesRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err)
		return
	}

	// 调用RPC服务获取风控规则列表
	rpcResp, err := config.UserClient.GetRiskRules(c, &user.GetRiskRulesReq{})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}
	response.Success(c, "风控规则列表获取成功", rpcResp.Rules)
}

// GetRiskRule 获取单个风控规则
// @Summary 获取单个风控规则
// @Description 根据规则ID获取单个风控规则详情
// @Tags 风控规则引擎
// @Accept json
// @Produce json
// @Param id path int true "规则ID"
// @Success 200 {object} response.SuccessResponse{data=request.RiskControlRule}
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/risk_control/rules/{id} [get]
func GetRiskRule(c *gin.Context) {
	var req request.GetRiskRuleRequest
	if err := c.ShouldBindUri(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err)
		return
	}

	// 调用RPC服务获取单个风控规则
	rpcResp, err := config.UserClient.GetRiskRule(c, &user.GetRiskRuleReq{
		Id: uint64(req.ID),
	})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}
	response.Success(c, "风控规则详情获取成功", rpcResp.Rule)
}

// CreateRiskRule 创建风控规则
// @Summary 创建风控规则
// @Description 创建新的风控规则
// @Tags 风控规则引擎
// @Accept json
// @Produce json
// @Param request body request.CreateRiskRuleRequest true "请求参数"
// @Success 200 {object} response.SuccessResponse
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/risk_control/rules [post]
func CreateRiskRule(c *gin.Context) {
	var req request.CreateRiskRuleRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err)
		return
	}

	// 调用RPC服务创建风控规则
	rpcResp, err := config.UserClient.CreateRiskRule(c, &user.CreateRiskRuleReq{})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}
	response.Success(c, "风控规则创建成功", rpcResp.Rule)
}

// UpdateRiskRule 更新风控规则
// @Summary 更新风控规则
// @Description 更新指定ID的风控规则
// @Tags 风控规则引擎
// @Accept json
// @Produce json
// @Param id path int true "规则ID"
// @Param request body request.UpdateRiskRuleRequest true "请求参数"
// @Success 200 {object} response.SuccessResponse
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/risk_control/rules/{id} [put]
func UpdateRiskRule(c *gin.Context) {
	var req request.UpdateRiskRuleRequest
	if err := c.ShouldBindUri(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err)
		return
	}
	// 调用RPC服务更新风控规则
	var rpcResp, err = config.UserClient.UpdateRiskRule(c, &user.UpdateRiskRuleReq{
		Rule: nil,
	})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}
	response.Success(c, "风控规则更新成功", rpcResp.Rule)
}

// DeleteRiskRule 删除风控规则
// @Summary 删除风控规则
// @Description 删除指定ID的风控规则
// @Tags 风控规则引擎
// @Accept json
// @Produce json
// @Param id path int true "规则ID"
// @Success 200 {object} response.SuccessResponse
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/risk_control/rules/{id} [delete]
func DeleteRiskRule(c *gin.Context) {
	var req request.DeleteRiskRuleRequest
	if err := c.ShouldBindUri(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err)
		return
	}

	// 调用RPC服务删除风控规则
	rpcResp, err := config.UserClient.DeleteRiskRule(c, &user.DeleteRiskRuleReq{
		Id: uint64(req.ID),
	})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}
	response.Success(c, "风控规则删除成功", rpcResp)
}

// ToggleRiskRuleStatus 切换风控规则状态
// @Summary 切换风控规则状态
// @Description 切换指定ID的风控规则启用/禁用状态
// @Tags 风控规则引擎
// @Accept json
// @Produce json
// @Param id path int true "规则ID"
// @Param request body request.ToggleRiskRuleStatusRequest true "请求参数"
// @Success 200 {object} response.SuccessResponse
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/risk_control/rules/{id}/status [put]
func ToggleRiskRuleStatus(c *gin.Context) {
	var req request.ToggleRiskRuleStatusRequest
	if err := c.ShouldBindUri(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err)
		return
	}
	// 调用RPC服务切换风控规则状态
	rpcResp, err := config.UserClient.ToggleRiskRuleStatus(c, &user.ToggleRiskRuleStatusReq{
		Id:       uint64(req.ID),
		IsActive: false,
	})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}
	response.Success(c, "风控规则状态切换成功", rpcResp)
}

// ExecuteRiskCheck 执行风控检查
// @Summary 执行风控检查
// @Description 对指定操作执行风控检查
// @Tags 风控规则引擎
// @Accept json
// @Produce json
// @Param request body request.ExecuteRiskCheckRequest true "请求参数"
// @Success 200 {object} response.SuccessResponse{data=request.RiskCheckResponse}
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/risk_control/check [post]
func ExecuteRiskCheck(c *gin.Context) {
	var req request.ExecuteRiskCheckRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err)
		return
	}

	// 调用RPC服务执行风控检查
	rpcResp, err := config.UserClient.ExecuteRiskCheck(c, &user.RiskCheckRequest{
		UserId: uint64(req.UserID),
		Scene:  req.TransactionID,
	})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}
	response.Success(c, "风控检查结果获取成功", rpcResp)
}

// GetRiskLogs 获取风控日志
// @Summary 获取风控日志
// @Description 根据条件获取风控日志列表
// @Tags 风控规则引擎
// @Accept json
// @Produce json
// @Param request query request.GetRiskLogsRequest true "请求参数"
// @Success 200 {object} response.SuccessResponse{data=[]request.RiskLog}
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/risk_control/logs [get]
func GetRiskLogs(c *gin.Context) {
	var req request.GetRiskLogsRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err)
		return
	}

	// 调用RPC服务获取风控日志
	rpcResp, err := config.UserClient.GetRiskLogs(c, &user.GetRiskLogsReq{
		UserId:        uint64(req.UserId),
		RuleName:      req.RuleName,
		TriggeredOnly: false,
		Page:          int32(req.Page),
		PageSize:      int32(req.PageSize),
	})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}
	response.Success(c, "风控日志获取成功", rpcResp.Logs)
}
